/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.cash.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.common.utils.DateUtils;
import com.chlitina.store.modules.cash.dao.StoreConsumeNewCardDao;
import com.chlitina.store.modules.cash.entity.StoreConsume;
import com.chlitina.store.modules.cash.entity.StoreConsumeNewCard;
import com.chlitina.store.modules.employee.entity.Employee;
import com.chlitina.store.modules.expert.entity.Care;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 新开卡业务消费Service
 * @author liujiang
 * @version 2013-11-24
 */
@Component
@Transactional(readOnly = true)
public class StoreConsumeNewCardService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(StoreConsumeNewCardService.class);
	
	@Autowired
	private StoreConsumeNewCardDao storeConsumeNewCardDao;
	@Autowired
	private StoreConsumeService storeConsumeService;
	
	public StoreConsumeNewCard get(Long id) {
		return storeConsumeNewCardDao.findOne(id);
	}
	
	public Page<StoreConsumeNewCard> find(Page<StoreConsumeNewCard> page, StoreConsumeNewCard storeConsumeNewCard) {
		DetachedCriteria dc = storeConsumeNewCardDao.createDetachedCriteria();
		dc.addOrder(Order.desc("id"));
		return storeConsumeNewCardDao.find(page, dc);
	}
	
	public Map<Integer, Integer> getByEmployeeSatisfaction(Employee employee, Date start, Date end) {
		List<Long> existConsumeIds = Lists.newArrayList();
		Map<Integer, Integer> map = Maps.newHashMap();
		map.put(StoreConsume.TYPE_SATISFACTIOIN_1, 0);
		map.put(StoreConsume.TYPE_SATISFACTIOIN_2, 0);
		map.put(StoreConsume.TYPE_SATISFACTIOIN_3, 0);
		map.put(StoreConsume.TYPE_SATISFACTIOIN_4, 0);
		map.put(StoreConsume.TYPE_SATISFACTIOIN_5, 0);
		List<StoreConsumeNewCard> byEmployee = getByEmployee(employee, start, end);
		for (StoreConsumeNewCard storeConsumeNewCard : byEmployee) {
			Long id = storeConsumeNewCard.getStoreConsume().getId();
			if(existConsumeIds.contains(id)){
				continue;
			}
			existConsumeIds.add(id);
			int type = storeConsumeNewCard.getStoreConsume().getSatisfaction();
			if(type != 0) {
				map.put(type, (map.get(type)+1));
			}
		}
		return map;
	}
	
	public float getByEmployeeCost(Employee employee, Date start, Date end) {
		float money = 0f;
		List<StoreConsumeNewCard> byEmployee = getByEmployee(employee, start, end);
		for (StoreConsumeNewCard storeConsumeNewCard : byEmployee) {
			if(!storeConsumeService.ifDebtByConsume(storeConsumeNewCard.getStoreConsume().getId())){
				money += storeConsumeNewCard.getReal_cost();
			}
		}
		return money;
	}
	
	public float getByEmployeeBonus(Employee employee, Date start, Date end) {
		float money = 0f;
		List<StoreConsumeNewCard> byEmployee = getByEmployee(employee, start, end);
		for (StoreConsumeNewCard storeConsumeNewCard : byEmployee) {
			if(!storeConsumeService.ifDebtByConsume(storeConsumeNewCard.getStoreConsume().getId())){
				money += storeConsumeNewCard.getBonus();
			}
		}
		return money;
	}
	
	public List<StoreConsumeNewCard> getByEmployee(Employee employee, Date start, Date end) {
		DetachedCriteria dc = storeConsumeNewCardDao.createDetachedCriteria();
		dc.add(Restrictions.eq("employee", employee));
		dc.createAlias("storeConsume", "sc");
		if(start != null && end != null) {
			dc.add(Restrictions.between("sc.consume_Time", start, end));
		}
		if(start == null && end != null) {
			dc.add(Restrictions.le("sc.consume_Time", end));
		}
		if(start != null && end == null) {
			dc.add(Restrictions.ge("sc.consume_Time", start));
		}
		dc.add(Restrictions.eq("sc.confirmed", StoreConsume.TYPE_CONFIRMED));
		return storeConsumeNewCardDao.find(dc);
	}
	
	@Transactional(readOnly = false)
	public void save(StoreConsumeNewCard storeConsumeNewCard) {
		storeConsumeNewCardDao.save(storeConsumeNewCard);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		storeConsumeNewCardDao.delete(id);
	}
	
}
